package com.mgq.juc.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author MaGuangQi
 * super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
 *               new DelayedWorkQueue());
 * @date 2021-12-22 14:19
 **/
@Slf4j
public class TestSchedulerThreadPool {

    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
       // method1(scheduledExecutorService);
        //每隔3秒执行一次
        /*scheduledExecutorService.scheduleAtFixedRate(()->{
            log.debug("----------");
        },1,3,TimeUnit.SECONDS);*/

        scheduledExecutorService.scheduleWithFixedDelay(()->{
            log.debug("----------");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },1,1,TimeUnit.SECONDS);
    }

    private static void method1(ScheduledExecutorService scheduledExecutorService) {
        //延迟1秒执行
        scheduledExecutorService.schedule(()->{
            log.debug("hello world 1");
        },1, TimeUnit.SECONDS);
        scheduledExecutorService.schedule(()->{
            log.debug("hello world 2");
        },2, TimeUnit.SECONDS);
        scheduledExecutorService.schedule(()->{
            log.debug("hello world 3");
        },3, TimeUnit.SECONDS);
    }
}
